package cn.edu.cxcyzx.supervisor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

public class UploadMultipartResolver extends CommonsMultipartResolver {
	
	@Autowired
	private UploadListener uploadListener;

	public void setFileUploadProgressListener(UploadListener uploadListener){
		this.uploadListener=uploadListener;
	}
	@Override
	protected MultipartParsingResult parseRequest(HttpServletRequest request){
		 String encoding = determineEncoding(request);    
	     FileUpload fileUpload = prepareFileUpload(encoding);    
	     uploadListener.setSession(request.getSession());    
	     fileUpload.setProgressListener(uploadListener);    
	     try {    
	         List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);    
	         return parseFileItems(fileItems, encoding);    
	     }    
	     catch (FileUploadBase.SizeLimitExceededException ex) {    
	         throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);    
	     }    
	     catch (FileUploadException ex) {    
	         throw new MultipartException("Could not parse multipart servlet request", ex);    
	     }  
	}
	
	
	
}
